#pragma once 
#include "Socket.hpp"
#include "InetAddr.hpp"
#include "ThreadPool.hpp"
using namespace MyThreadPoolMudule;
using handler_t = std::function<void(SockPtr,InetAddr)>;
using task_t = std::function<void()>;
class TcpServer
{
public:
    TcpServer(uint16_t port)
    :_port(port),
    _socket(std::make_shared<TcpSocket>())
    {
    }
    void Insert(handler_t handler){
        _handler = handler;
    }
    void Build(){
        _socket->BuildTcpSocket(_port);
    }
    void Loop(){
        while(1){
            //获取连接
            struct sockaddr_in peer;
            SockPtr ptr = _socket->acceptor(&peer);
            InetAddr client(peer);
            ThreadPool<task_t>::GetInstance()->Enque([this,ptr,client](){
                this->_handler(ptr,client);
            });
        }
    }
    ~TcpServer(){
    }
private:
    std::shared_ptr<Socket> _socket;
    uint16_t _port;
    handler_t _handler;
};